package com.block.chlunion.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.chlunion.service.IChannelUnionBaseInfoService;
import com.block.db.entity.ChannelUnionBaseInfo;
import com.block.db.mapper.ChannelUnionBaseInfoMapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 渠道联登基本信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-06-08
 */
@Service
public class ChannelUnionBaseInfoServiceImpl extends ServiceImpl<ChannelUnionBaseInfoMapper, ChannelUnionBaseInfo> implements IChannelUnionBaseInfoService {

    @Resource
    private RedisTemplate<String, ChannelUnionBaseInfo> redisUnionBaseInfoTemplate;

    private  static  final  String CHANNLEUNION_KEY_PREFIX="CHANNLEUNION:CODE:";
    private  static  final  Long CHANNLEUNION_KEY_TIME_OUT=4L;

    @Override
    public ChannelUnionBaseInfo getUnionBaseInfoByType(String channelCode){
        String redisKey = CHANNLEUNION_KEY_PREFIX+channelCode;
        ChannelUnionBaseInfo unionBaseInfo= redisUnionBaseInfoTemplate.opsForValue().get(redisKey);
        if(ObjectUtil.isEmpty(unionBaseInfo)){
            unionBaseInfo = this.baseMapper.getEntityByChannelCode(channelCode);
            if(unionBaseInfo!=null) {
                redisUnionBaseInfoTemplate.opsForValue().set(redisKey, unionBaseInfo, CHANNLEUNION_KEY_TIME_OUT, TimeUnit.HOURS);
            }
        }
        return unionBaseInfo;
    }
}
